﻿//-----------------------------------------------------------------------
// <copyright file="DependencyResolverFactory.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//---------------------------------------------------------------------
namespace Deixei.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Dependency resolver factory
    /// </summary>
    public class DependencyResolverFactory : IDependencyResolverFactory
    {
        /// <summary>
        /// Resolver type
        /// </summary>
        private readonly Type resolverType;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="DependencyResolverFactory" /> class.
        /// </summary>
        public DependencyResolverFactory()
            : this(ConfigurationManager.AppSettings["DependencyResolverTypeName"])
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DependencyResolverFactory" /> class.
        /// </summary>
        /// <param name="resolverTypeName">Name of the resolver type.</param>
        /// <exception cref="System.ArgumentNullException">Argument Null Exception</exception>
        public DependencyResolverFactory(string resolverTypeName)
        {
            if (string.IsNullOrEmpty(resolverTypeName))
            {
                throw new ArgumentNullException("resolverTypeName");
            }

            this.resolverType = Type.GetType(resolverTypeName, true, true);
        }

        /// <summary>
        /// Create dependency resolver
        /// </summary>
        /// <returns>Dependency resolver</returns>
        public IDependencyResolver CreateInstance()
        {
            return Activator.CreateInstance(this.resolverType) as IDependencyResolver;
        }
    }
}
